import axios from "axios"
import { Dispatch } from "redux"
import { CarRowState } from "../car/carReducer"

interface UserState {
	addressList: AddressState[],
	avator: string,
	id: string,
	nickname: string,
	orderList: OrderDetail[],
	token: string,
	username: string
}

export interface AddressState {
	id: string
	nickname: string
	tel: string
	address: string
	pos: { lng: number, lat: number }
}

export type OrderState = '待付款' | '待收货' | '退换/售后' | '已关闭'

export interface OrderDetail {
	id: string,
	price: number,
	title: string,
	addressDetail: AddressState,
	createDateTime: number,
	projects: CarRowState[],
	state: OrderState
}

const initState: UserState = {
	avator: '',
	id: '',
	nickname: '',
	token: '',
	username: '',

	addressList: [],
	orderList: [],
}

const LOGINSUCCESS = 'LOGINSUCCESS'
const QUITLOGIN = 'QUITLOGIN'
const CREATEORDER = 'CREATEORDER'
const TRADESUCCESS = 'TRADESUCCESS'
const DELETEADDRESS = 'DELETEADDRESS'
const CLOSEORDER = 'CLOSEORDER'
const MODIFYORDERADDRESSDETAIL = 'MODIFYORDERADDRESSDETAIL'

interface ModifyOrderAddressDetailAction {
	type: typeof MODIFYORDERADDRESSDETAIL
	payload: { addressDetail: AddressState, id: string }
}

export function modifyOrderAddressDetailActionCreator(payload: { addressDetail: AddressState, id: string }): ModifyOrderAddressDetailAction {
	return { type: MODIFYORDERADDRESSDETAIL, payload: payload }
}

interface CloseOrderAction {
	type: typeof CLOSEORDER
	payload: OrderDetail
}

export function closeOrderActionCreator(payload: OrderDetail): CloseOrderAction {
	return { type: CLOSEORDER, payload }
}

interface DeleteAddressAction {
	type: typeof DELETEADDRESS
	payload: AddressState
}


export function deleteAddressActionCreator(payload: AddressState) {
	return async (dispatch: Dispatch) => {
		try {
			const resp = await axios.delete('/api/address/list', { params: { id: payload.id } })
			console.log(resp.data)
			if (resp.data.code === 200)
				dispatch({ type: DELETEADDRESS, payload })
			else
				console.log('删除失败')
		} catch {
			console.log('出错了！！')
		}

	}
}


interface TradeSuccessAction {
	type: typeof TRADESUCCESS
	payload: string
}
export function tradeSuccessActionCreator(payload: string): TradeSuccessAction {
	return { type: TRADESUCCESS, payload }
}


interface CreateOrderAction {
	type: typeof CREATEORDER
	payload: OrderDetail
}

export function createOrderActionCreator(payload: OrderDetail) {
	return async (dispatch: Dispatch) => {
		try {
			const resp = await axios.post('/api/order/list', { data: payload })
			dispatch({ type: CREATEORDER, payload })
		} catch {
			console.log('出错了！！')
		}

	}
}


interface LoginSuccessAction {
	type: typeof LOGINSUCCESS
	payload: UserState
}

export function loginSuccessActionCreator(payload: UserState): LoginSuccessAction {
	return { type: LOGINSUCCESS, payload }
}

interface QuitLoginAction {
	type: typeof QUITLOGIN
}

export function quitLoginActionCreator(): QuitLoginAction {
	return { type: QUITLOGIN }
}


type CarAction = LoginSuccessAction | QuitLoginAction | CreateOrderAction | TradeSuccessAction | DeleteAddressAction | CloseOrderAction | ModifyOrderAddressDetailAction

const userReducer = (state = initState, action: CarAction): UserState => {
	const { type } = action
	switch (type) {
		case LOGINSUCCESS: {
			return {
				...state,
				...action.payload
			}
		}
		case QUITLOGIN: {
			localStorage.removeItem('token')
			return {
				addressList: [],
				avator: '',
				id: '',
				nickname: '',
				orderList: [],
				token: '',
				username: ''
			}
		}
		case CREATEORDER: {
			return {
				...state,
				orderList: [action.payload, ...state.orderList]
			}
		}
		case TRADESUCCESS: {
			const item = state.orderList.find(v => v.id === action.payload);
			if (item) {
				item.state = '待收货'
			}
			return {
				...state,
				orderList: [...state.orderList]
			}
		}
		case DELETEADDRESS: {
			return {
				...state,
				addressList: state.addressList.filter(v => v.id !== action.payload.id)
			}
		}
		case CLOSEORDER: {
			const item = state.orderList.find(v => v.id === action.payload.id);
			if (item) {
				item.state = '已关闭'
			}
			return {
				...state,
				orderList: [...state.orderList]
			}
		}
		case MODIFYORDERADDRESSDETAIL: {
			const item = state.orderList.find(v => v.id === action.payload.id)
			if (item) {
				Object.assign(item.addressDetail, { ...action.payload.addressDetail });
			}


			return {
				...state,
				orderList: [...state.orderList]
			}
		}
		default: return state
	}
}


export default userReducer